///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_THREADS_CSYNCQUEUE_H__
#define __BUOLA_THREADS_CSYNCQUEUE_H__

#include <buola/threads/thread.h>
#include <deque>
#include <buola/threads/condition_variable.h>

namespace buola {

///\ingroup utility threads
///@{

////////////////////////////////////////////////////////////////////////////

/// synchronized queue

/// A synchronized queue that holds objects of type tType. Objects can be
/// added to the queue in any thread, and retrieved in a safe way from
/// another thread. If no objects are available, the call blocks until
/// some objects are added to the queue.
///
////////////////////////////////////////////////////////////////////////////

template<typename tType> class CSyncQueue
{
public:
//! \name constructor and destructor
//@{
    CSyncQueue()
    {
    }

    ~CSyncQueue()
    {
    }
//@}

    //! returns number of objects in the queue
    std::size_t size() const
    {
        lock_guard<mutex> lLock(mCondMutex);
        return mQueue.size();
    }

    //! returns true if the queue is empty
    std::size_t empty() const
    {
        lock_guard<mutex> lLock(mCondMutex);
        return mQueue.empty();
    }

    //! gets the oldest object in the queue
    tType Get()
    {
        unique_lock<mutex> lLock(mCondMutex);
        while(mQueue.empty()) mCondVar.wait(lLock);
        tType lReturn=mQueue.back();
        mQueue.pop_back();
        return lReturn;
    }

    //! tries to get the oldest object in the queue. Returns tType() if empty
    tType TryGet()
    {
        lock_guard<mutex> lLock(mCondMutex);
        if(mQueue.empty())
            return tType();

        tType lReturn=mQueue.back();
        mQueue.pop_back();
        return lReturn;
    }

    //! discards all objects
    void DiscardAll()
    {
        lock_guard<mutex> lLock(mCondMutex);
        mQueue.clear();
    }

    //! pushes a new object into the queue
    std::size_t Add(const tType &pData)
    {
        lock_guard<mutex> lLock(mCondMutex);
        mQueue.push_front(pData);

        mCondVar.notify_one();

        return mQueue.size();
    }

private:
    condition_variable mCondVar;
    mutable mutex mCondMutex;

    std::deque<tType> mQueue;
};

///@}

/*namespace buola*/ }

#endif
